﻿if (_MapCity == "undefined" || _MapCity == "")
    _MapCity = "广州";
var map = new BMap.Map("allmap", { minZoom: 8, maxZoom: 19 });
map.enableScrollWheelZoom();
map.enableContinuousZoom();
map.centerAndZoom(_MapCity, 16);
map.addControl(new BMap.NavigationControl());

var myGeo = new Geocoder();

//#region 添加圆时需要的圆形区域计算
function getSquareBounds(centerPoi, r) {
    var a = Math.sqrt(2) * r; //正方形边长
    mPoi = getMecator(centerPoi);
    var x0 = mPoi.x, y0 = mPoi.y;
    var x1 = x0 + a / 2, y1 = y0 + a / 2; //东北点
    var x2 = x0 - a / 2, y2 = y0 - a / 2; //西南点
    var ne = getPoi(new BMap.Pixel(x1, y1)), sw = getPoi(new BMap.Pixel(x2, y2));
    return new BMap.Bounds(sw, ne);

    //根据球面坐标获得平面坐标。
    function getMecator(poi) {

        return map.getMapType().getProjection().lngLatToPoint(poi);
    }
    //根据平面坐标获得球面坐标。
    function getPoi(mecator) {

        return map.getMapType().getProjection().pointToLngLat(mecator);
    }
}
function Initalize() {
    //移动到指定位置
    function SetLocation(point) {
        map.panTo(new BMap.Point(point.lng, point.lat));
        map.addControl(new BMap.ScaleControl({ anchor: BMAP_ANCHOR_BOTTOM_LEFT }));
    }
    //画一个标记
    function MakeMark(point, text, mouseoverText) {
        function ComplexCustomOverlay(point, text, mouseoverText) {
            this._point = point;
            this._text = text;
            this._overText = mouseoverText;
        }
        ComplexCustomOverlay.prototype = new BMap.Overlay();
        ComplexCustomOverlay.prototype.initialize = function (map) {
            this._map = map;
            var div = this._div = document.createElement("div");
            div.style.position = "absolute";
            div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
            div.style.backgroundColor = "#EE5D5B";
            div.style.border = "1px solid #BC3B3A";
            div.style.color = "white";
            div.style.height = "18px";
            div.style.padding = "2px";
            div.style.lineHeight = "18px";
            div.style.whiteSpace = "nowrap";
            div.style.MozUserSelect = "none";
            div.style.fontSize = "12px"
            var span = this._span = document.createElement("span");
            div.appendChild(span);
            span.appendChild(document.createTextNode(this._text));
            var that = this;

            var arrow = this._arrow = document.createElement("div");
            arrow.style.background = "url(http://map.baidu.com/fwmap/upload/r/map/fwmap/static/house/images/label.png) no-repeat";
            arrow.style.position = "absolute";
            arrow.style.width = "11px";
            arrow.style.height = "10px";
            arrow.style.top = "22px";
            arrow.style.left = "10px";
            arrow.style.overflow = "hidden";
            div.appendChild(arrow);
            div.onmouseover = function () {
                this.style.backgroundColor = "#6BADCA";
                this.style.borderColor = "#0000ff";
                this.getElementsByTagName("span")[0].innerHTML = that._overText;
                arrow.style.backgroundPosition = "0px -20px";
            }

            div.onmouseout = function () {
                this.style.backgroundColor = "#EE5D5B";
                this.style.borderColor = "#BC3B3A";
                this.getElementsByTagName("span")[0].innerHTML = that._text;
                arrow.style.backgroundPosition = "0px 0px";
            }
            map.getPanes().labelPane.appendChild(div);
            return div;
        }
        ComplexCustomOverlay.prototype.draw = function () {
            var map = this._map;
            var pixel = map.pointToOverlayPixel(this._point);
            this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
            this._div.style.top = pixel.y - 30 + "px";
        }
        var txt = _MapAddress, mouseoverTxt = txt; //调数据的时候再套上去
        var myCompOverlay = new ComplexCustomOverlay(new BMap.Point(point.lng, point.lat), _MapAddress, mouseoverTxt);
        map.addOverlay(myCompOverlay);
    }

    //补充地址
    if ("undefined" == typeof _MapDetailAddress) {
        _MapDetailAddress = '';
    }
    _MapDetailAddress = _MapCity + _MapDetailAddress + _MapAddress;

//    if (_MapDetail_longitude != 0 && _MapDetail_latitude != 0) {
//        var point = {};
//        point.lng = _MapDetail_longitude;
//        point.lat = _MapDetail_latitude;
//        setTimeout(function () {
//            SetLocation(point);
//            MakeMark(new BMap.Point(point.lng, point.lat), _MapAddress, _MapAddress);
//        }, 700);
//    } else {
        myGeo.getPoint(_MapDetailAddress, _MapCity, "myGeo", function (e) {
            if (e.result) {
                var point = e.result.location;
                setTimeout(function () {
                    SetLocation(point);
                    MakeMark(new BMap.Point(point.lng, point.lat), _MapAddress, _MapAddress);
                }, 500);
            }
        });
//    }




}
//获取周边 信息
function GetNearInfo(infoType, isAdd, callback) {
    function GetAround(point) {
        map.panTo(new BMap.Point(point.lng, point.lat)); //调整地图视野
        map.addControl(new BMap.ScaleControl({ anchor: BMAP_ANCHOR_BOTTOM_LEFT }));
        var mPoint = new BMap.Point(point.lng, point.lat);
        var circle = new BMap.Circle(mPoint, 2000, { fillColor: "blue", strokeWeight: 1, fillOpacity: 0.01, strokeOpacity: 0.01 });
        map.addOverlay(circle);
        bounds = getSquareBounds(circle.getCenter(), circle.getRadius());
        var local1 = new BMap.LocalSearch(map, { renderOptions: { map: map, autoViewport: false, selectFirstResult: false} });
        local1.searchInBounds(infoType, bounds);

        if (!isAdd) {
            map.clearOverlays();
        }
        local1.setMarkersSetCallback(function (results) {
            //周边信息存放 数组
            var nearInfo = [];
            for (var i = 0; i < results.length; i++) {
                var jsonobj = results[i]; //附近环境坐标  
                var item = {};

                var distance = Math.floor(Number(getDistance(e.result.location, jsonobj.point)));
                if (distance > 2000)
                    continue;
                item["title"] = jsonobj.title;
                item["distance"] = distance;
                item["url"] = jsonobj.url;
                item["address"] = jsonobj.address;
                item["city"] = jsonobj.city;
                item["phoneNumber"] = jsonobj.phoneNumber;
                item["postcode"] = jsonobj.postcode;
                nearInfo.push(item);
            } //for var i =0 
            //对返回数组进行冒泡排序
            for (var i = 0; i < nearInfo.length; i++) {
                for (var j = i + 1; j < nearInfo.length; j++) {
                    if (nearInfo[i].distance > nearInfo[j].distance) {
                        var temp = nearInfo[j];
                        nearInfo[j] = nearInfo[i];
                        nearInfo[i] = temp;
                    }
                }
            }
            callback(nearInfo);
        });
    }

//    if (_MapDetail_longitude != 0 && _MapDetail_latitude != 0) {
//        var point = {};
//        point.lng = _MapDetail_longitude;
//        point.lat = _MapDetail_latitude;
//        GetAround(point);
//    } else {
        myGeo.getPoint(_MapDetailAddress, _MapCity, "myGeo", function (e) {
            if (e.result) {
                var point = e.result.location;
                GetAround(point);
            }
        });
//    }


}

Initalize();